<?php
App::import('Controller', 'Upload');
App::uses('GenericUtil', 'Lib');


class ImportController extends AppController {
    var $helpers = array ('Html','Form','Session');
    //var $modelClass = 'Multa';
    
    public function beforeFilter(){
		parent::beforeFilter();
        // $this->Auth->allow('import_soccerhistory','import_topscorers');
    }

	public function xml($id) {
	    $this->layout="admin";
		$this->loadModel("Attach");		
	    $ds = $this->Attach->getDataSource();
		$ds->begin();
		try {
			$this->loadModel("Xml");
	        $this->Xml->recursive = -1;
			$this->Xml->read(null,$id);
			$this->Attach->read(null,$this->Xml->data["Xml"]["attach_id"]);
			$this->{"import_".$this->Xml->data["Xml"]["type"]}($this->Attach->data["Attach"]["webpath"]);
			$this->Xml->set("stato",XML_IMPORTATO);
			$this->Xml->save();
			$ds->commit();
			$this->Session->setFlash(__("Importazione eseguita con successo."));
		} catch (Exception $e) {
			$ds->rollback();
	    	CakeLog::error($e->getMessage());
			$this->Session->setFlash(__("Formato xml non riconosciuto: ", $e->getMessage()),"default",null,"error");
		}
		$this->redirect($this->referer());
	}

	public function feed($id) {
	    $this->layout="admin";
		$this->loadModel("Feed");		
	    $ds = $this->Feed->getDataSource();
		$ds->begin();
		try {
	        $this->Feed->recursive = -1;
			$this->Feed->read(null,$id);
			$this->{"import_".$this->Feed->data["Feed"]["type"]}($this->Feed->data["Feed"]["url"]);
			$this->Feed->set("dt_import",date("Y/m/d H:i:s"));
			$this->Feed->save();
			$ds->commit();
			$this->Session->setFlash(__("Importazione eseguita con successo."));
		} catch (Exception $e) {
	    	CakeLog::error($e->getMessage());
			$ds->rollback();
			$this->Session->setFlash(__("Formato xml non riconosciuto: ", $e->getMessage()),"default",null,"error");
		}
		$this->redirect($this->referer());
	}

	public function soccernew($url) {

	}

	public function soccer_odds($url) {

	}
	
	public function standings($url) {

	}
	
	public function commentaries($url) {

	}
	
	
	public function soccerfixtures($feedid) {
		try {
			GenericUtil::soccerfixtures($feedid, $this);
		} catch (Exception $e) {
			CakeLog::error($e->getMessage(),$e);
		}
		$this->redirect($this->referer());
	}
	
	public function soccerhistory($url) {
		$this->soccerfixtures($url);
	}
	
	public function topscorers($url) {

	}

	private function import_teams($url,$teamId=null) {
		if (empty($teamId))
			$sxe = new SimpleXMLElement($url,0,true);
		else
			$sxe = new SimpleXMLElement($url."/".$teamId,0,true);
		$this->loadModel("Team");		
		$this->Team->set("id",$sxe->team->attributes()->id);
		$this->Team->set("is_national_team",$sxe->team->attributes()->is_national_team);
		$this->Team->set("name",$sxe->team->name);
		$this->Team->set("country",$sxe->team->country);
		$this->Team->set("founded",$sxe->team->founded);
		$this->Team->set("venue_name",$sxe->team->venue_name);
		$this->Team->set("venue_id",$sxe->team->venue_id);
		$this->Team->set("venue_surface",$sxe->team->venue_surface);
		$this->Team->set("venue_capacity",$sxe->team->venue_capacity);
		$this->Team->set("venue_image",$sxe->team->venue_image);
		$this->Team->set("image",$sxe->team->image);
		// $this->Team->set("coach",$sxe->team->coach->attributes()->name);
		$this->Team->save();
	}
	
}
?>